home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_dos.lha / dos / sphsrc / sph_cull.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  2.4 KB  |  83 lines

  1. #include "HEADERS.h"
  2. /**********************
  3.                       *
  4.  File                 * cull.c
  5.                       * backface culling unit
  6.                       *
  7.                       * changes p_normals in objects
  8.                       *
  9.                       *********************************************************/
  10.  
  11. /**********************
  12.                       *
  13.  Includes          *
  14.                       *
  15.                       *********************************************************/
  16.  
  17. #include <stdio.h>
  18. #include <assert.h>
  19.  
  20. #include "sphigslocal.h"
  21.  
  22. /**********************
  23.                       *
  24.  Function          * cull
  25.                       *
  26.                       * reduces the number of nodes in the objects list
  27.                       * through backface culling with the perspectivized
  28.                       * normals
  29.                       *
  30.                       *********************************************************/
  31.  
  32. void SPH__cull (view_spec *vs)
  33. {
  34.      obj *            current;    /* current object */
  35.      obj *            prev;        /* keep the previous for node removal */
  36.      MAT3hvec            vec1;        /* subtracted vector 1 */
  37.      MAT3hvec            vec2;        /* subtracted vector 2 */
  38.      MAT3hvec            canon_normal;    /* canonized normal to the face */
  39.      double            length;        /* used in normalize */
  40.      
  41.      assert( vs != NULL );
  42.      
  43.      if( vs->objects == NULL )
  44.       return;
  45.      
  46.      prev = NULL;
  47.      current = vs->objects;
  48.      
  49.      prev = NULL;
  50.      while( current != NULL ) {
  51.       
  52.       if (current->type == objFace) {
  53.            
  54.            /* Calculate normal */
  55.            MAT3_SUB_VEC (vec1,
  56.                  vs->npcVertices[current->data.face.points[1]],
  57.                  vs->npcVertices[current->data.face.points[0]]);
  58.            MAT3_SUB_VEC (vec2,
  59.                  vs->npcVertices[current->data.face.points[2]],
  60.                  vs->npcVertices[current->data.face.points[1]]);
  61.            vec1[3] = 1;
  62.            vec2[3] = 1;
  63.            MAT3_NORMALIZE_VEC( vec1, length );
  64.            MAT3_NORMALIZE_VEC( vec2, length );
  65.            MAT3cross_product (canon_normal, vec1, vec2);
  66.            canon_normal[3] = 1;
  67.            MAT3_NORMALIZE_VEC( canon_normal, length );
  68.            MAT3_COPY_VEC( current->p_normal, canon_normal );
  69.            current->p_normal[3] = 1;
  70.            
  71.            if( current->p_normal[Z] <= 0 ) {
  72.             if( prev == NULL )
  73.              vs->objects = current->next;
  74.             else
  75.              prev->next = current->next;
  76.            } else
  77.             prev = current;
  78.            
  79.            current = current -> next;
  80.       }
  81.      }
  82. }
  83.